package com.gitee.melin.bee.core.jdbc.dialect.impl;

import com.gitee.melin.bee.core.jdbc.dialect.AbstractJdbcDialect;
import com.gitee.melin.bee.core.jdbc.enums.DataSourceType;
import com.gitee.melin.bee.core.jdbc.relational.BeeTableType;
import com.gitee.melin.bee.core.jdbc.relational.ConnectionInfo;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public class ClickHouseJdbcDialect extends AbstractJdbcDialect {

    @Override
    public DataSourceType getDataSourceType() {
        return DataSourceType.CLICKHOUSE;
    }

    public ClickHouseJdbcDialect(ConnectionInfo connectionInfo) {
        super(connectionInfo);
    }

    public ClickHouseJdbcDialect(Connection connection) {
        super(connection);
    }

    @Override
    protected Driver getDriver() throws SQLException {
        return loadDriver(DataSourceType.CLICKHOUSE.getDriverClass());
    }

    @Override
    public List<String> getSchemas() {
        return super.getDatabases().stream().filter(this::filterSchema).collect(Collectors.toList());
    }

    @Override
    protected boolean filterSchema(String schemaName) {
        if (schemaName.equalsIgnoreCase("INFORMATION_SCHEMA") || schemaName.equalsIgnoreCase("system")) {
            return false;
        }
        return super.filterSchema(schemaName);
    }

    @Override
    protected String getSelectTablesSql(String tableName) {
        String sql = "SELECT database AS TABLE_SCHEMA,\n"
                + "       null AS TABLE_COMMENT,\n"
                + "       null AS TABLE_TYPE,\n"
                + "       null AS TABLE_PARTITION,\n"
                + "       null AS TABLE_ROWS,\n"
                + "       null AS DATA_SIZE,\n"
                + "       null AS CREATE_TIME,\n"
                + "       null AS UPDATE_TIME\n"
                + "FROM AS system.tables\n"
                + "WHERE database = :schemaName";
        if (StringUtils.isNotBlank(tableName)) {
            sql += " AND name = :tableName";
        }
        return sql;
    }

    @Override
    protected String getSelectColumnsSql() {
        return "SELECT `name`                 AS COLUMN_NAME,\n"
                + "       `type`                 AS COLUMN_TYPE,\n"
                + "       null                   AS DATA_TYPE,\n"
                + "       `comment`              AS COLUMN_COMMENT,\n"
                + "       null                   AS DEFAULT_VALUE,\n"
                + "       null                   AS IS_NULLABLE,\n"
                + "       numeric_precision      AS NUMERIC_PRECISION,\n"
                + "       numeric_scale          AS NUMERIC_SCALE,\n"
                + "       character_octet_length AS DATA_LENGTH,\n"
                + "       datetime_precision     AS DATETIME_PRECISION\n"
                + "FROM system.columns\n"
                + "WHERE a.database = :schemaName\n"
                + "  and a.table = :tableName\n"
                + "order by a.position ASC";
    }

    @Override
    protected String getDDLColumnName(BeeTableType type) {
        throw new IllegalArgumentException("未定义 DDLColumnName");
    }
}
